﻿using TTGameEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AliveCard {

/// <summary>
/// 目前基本和GameEventID相同。 没有将TriggerType和GameEventID合并，是为了防止有单独的TriggerType。
/// </summary>
public enum TriggerTypeID {
    GT_StartGame,       //刚进入游戏场景时
    GT_EnterRoom,       //
    GT_ExitRoom,        //
    GT_Active,          //触发器绑定的对象激活时。触发器可能挂在对象上，而对象可能是在游戏中动态创建的。
    GT_EnterBattle,        //
    GT_ExitBattle,        //
    Count
}

public enum TriggerConfigID {
    TCfg_OnlyOnce,      //只触发一次。
}

public enum TriggerConditionID {
    TCon_None,          //无条件
    TCon_HasItem,       //拥有道具
    TCon_Option,        //有交互选项
    TCon_RoomID,        //人物所在房间
    TCon_TaskID,        //解锁剧情ID
    TCon_FightID,         //战斗相关
    Count
}

public enum TriggerActionID {
    TAct_UITips,        //UI提示文字(string:msg)
    TAct_ItemChange,    //道具变动
    TAct_Fight,         //进入战斗
    TAct_OptionChange,  //交互选项变动
    TAct_NPCTalk,       //NPC对话
    TAct_OpenUI,        //打开UI

    Count
}


public class GTConfig {
    public TriggerConfigID m_configID;
    public GTConfigData m_data;

    public void InitWithData(GTConfigData data) {
        m_data = data;
        if(data.m_configID != m_configID) {
            DebugUtil.LogErrorFormat("Error! GTConData is not match. ID[{0}] DataID[{1}]", m_configID, data.m_configID);
        }
        parseData();
    }

    public void parseData() {

    }

    public void SetupConfig() {
        switch(m_configID) {
            case TriggerConfigID.TCfg_OnlyOnce: {

                }
                break;

        }

    }
}

public abstract class GTCondition {
    public TriggerConditionID m_conditionID;
    public GTConData m_data;

    public void InitWithData(GTConData data) {
        m_data = data;
        if(data.m_conditionID != m_conditionID) {
            DebugUtil.LogErrorFormat("Error! ConditionID is not match. ID[{0}] DataID[{1}]", m_conditionID, data.m_conditionID);
        }
        parseData();
    }

    public virtual void parseData() { }

    public abstract bool IsConditionOK(object[] args);

}

public abstract class GTAction {
    public TriggerActionID m_actionID;
    public GTActData m_data;

    public void InitWithData(GTActData data) {
        m_data = data;
        if(data.m_actionID != m_actionID) {
            DebugUtil.LogErrorFormat("Error! ActionID is not match. ID[{0}] DataID[{1}]", m_actionID, data.m_actionID);
        }
        parseData();
    }

    public virtual void parseData() { }

    public abstract void DoIt();
}

public class GameTrigger {
    public string name;
    public bool isEnable = true;


    public bool m_isOnlyOnce;
    public TriggerTypeID m_eventType;
    public List<GTConfig> m_configs = new List<GTConfig>();
    public List<GTCondition> m_conditions = new List<GTCondition>();
    public List<GTAction> m_actions = new List<GTAction>();

    public object[] m_args;

    public static GameTrigger NewWithData(GameTriggerData gtData) {
        GameTrigger newTri = new GameTrigger();
        newTri.InitWithData(gtData);
        return newTri;
    }

    public void InitWithData(GameTriggerData data) {
        m_conditions.Clear();
        m_actions.Clear();
        this.name = data.name;
        this.m_eventType = data.m_eventType;
        this.m_isOnlyOnce = data.m_isOnlyOnce;

        foreach(GTConData conData in data.m_conditions) {
            m_conditions.Add(GenGameTriggerCondition(conData));
        }

        foreach(GTActData actData in data.m_actions) {
            m_actions.Add(GenGameTriggerAction(actData));
        }

    }

    public void SetArgs(object[] args) {
        m_args = args;
    }

    System.Type[] conTypes = {
        typeof(GTConNone),
        typeof(GTConHasItem),
        typeof(GTConOption),
        typeof(GTConRoomID),
        typeof(GTConTaskID),
        typeof(GTConFight),
    };

    System.Type[] actTypes = {
        typeof(GTActUITips),
        typeof(GTActItemChange),
        typeof(GTActFight),
        typeof(GTActOptionChange),
        typeof(GTActNPCTalk),
        typeof(GTActOpenUI),
    };
    public GTCondition GenGameTriggerCondition(GTConData data) {
        System.Type t = conTypes[(int)data.m_conditionID];
        GTCondition newCon = (GTCondition)Activator.CreateInstance(t);
        newCon.InitWithData(data);
        return newCon;
    }

    public GTAction GenGameTriggerAction(GTActData data) {
        System.Type t = actTypes[(int)data.m_actionID];
        GTAction newAct = (GTAction)t.New();
        newAct.InitWithData(data);
        return newAct;
    }
}

#region GTCondtions
/// <summary>
/// 无条件
/// </summary>
public class GTConNone : GTCondition {

    public GTConNone() {
        m_conditionID = TriggerConditionID.TCon_None;
    }

    public override bool IsConditionOK(object[] args) {
        return true;
    }
}

/// <summary>
/// 拥有道具
/// </summary>
public class GTConHasItem : GTCondition {
    public GTConHasItem() {
        m_conditionID = TriggerConditionID.TCon_HasItem;
    }
    public override bool IsConditionOK(object[] args) {
        throw new NotImplementedException();
    }
}
/// <summary>
/// 有交互选项
/// </summary>
public class GTConOption : GTCondition {
    public GTConOption() {
        m_conditionID = TriggerConditionID.TCon_Option;
    }
    public override bool IsConditionOK(object[] args) {
        throw new NotImplementedException();
    }
}
/// <summary>
/// 人物所在房间
/// </summary>
public class GTConRoomID : GTCondition {

    public int m_ConRoomID = 0;

    public GTConRoomID() {
        m_conditionID = TriggerConditionID.TCon_RoomID;
    }

    public override void parseData() {
        m_ConRoomID = Convert.ToInt32(m_data.m_argStr);
    }

    public override bool IsConditionOK(object[] args) {
        if(RoomManager.Ins.CurRoomID == m_ConRoomID) {
            return true;
        }
        return false;
    }
}
/// <summary>
/// 解锁剧情ID
/// </summary>
public class GTConTaskID : GTCondition {
    public GTConTaskID() {
        m_conditionID = TriggerConditionID.TCon_TaskID;
    }
    public override bool IsConditionOK(object[] args) {
        throw new NotImplementedException();
    }
}
/// <summary>
/// 检测战斗ID
///
/// argStr:
///     fightID(int)
/// </summary>
public class GTConFight : GTCondition {
    int conFightID = -1;
    public GTConFight() {
        m_conditionID = TriggerConditionID.TCon_FightID;
    }

    public override void parseData() {
        conFightID = m_data.m_argStr.intValue();
    }

    public override bool IsConditionOK(object[] args) {
        if((int)args[0] == conFightID) {
            return true;
        }
        return false;
    }
}

#endregion


#region GTActions
/// <summary>
/// UI提示文字
///
/// data:
/// tipType(int)|msg(string)
/// </summary>
public class GTActUITips : GTAction {
    int m_uiTipType;
    string m_msg;
    public GTActUITips() {
        m_actionID = TriggerActionID.TAct_UITips;
    }

    public override void parseData() {
        string[] args = m_data.m_argStr.Split('|');
// #if UNITY_EDITOR
//         if(args.Length < 2) {
//             DebugUtil.LogErrorFormat("[TAct_UITips] Action参数数量错误。");
//         }
// #endif
        m_uiTipType = (int)UIFormID.UI_FloatMsg;
        m_msg = args[0];
        if(args.Length > 1) m_uiTipType = args[1].intValue();
    }

    public override void DoIt() {
        switch((UIFormID)m_uiTipType) {
            case UIFormID.UI_FloatMsg:
                UIHelper.UIFloatMsg(m_msg);
                break;
            case UIFormID.UI_TipsMsg:
                UIHelper.UITipsMsg(m_msg);
                break;
            default:
                UIHelper.UIFloatMsg(m_msg);
                break;

        }
    }
}
/// <summary>
/// 道具变动
/// </summary>
public class GTActItemChange : GTAction {
    public GTActItemChange() {
        m_actionID = TriggerActionID.TAct_ItemChange;
    }
    public override void DoIt() {
        throw new NotImplementedException();
    }
}
/// <summary>
/// 进入战斗
/// </summary>
public class GTActFight : GTAction {
    int battleID;

    public GTActFight() {
        m_actionID = TriggerActionID.TAct_Fight;
    }

    public override void parseData() {
        battleID = m_data.m_argStr.intValue();
    }

    public override void DoIt() {
        //mark:todo
        string msg = string.Format("Test:进入战斗！battleID[{0}]", battleID);
        UIHelper.UIFloatMsg(msg);
        DebugUtil.LogFormat(msg);
    }
}
/// <summary>
/// 交互选项变动
/// </summary>
public class GTActOptionChange : GTAction {
    OptionID m_ConOptionID = OptionID.OptionID_None;
    public GTActOptionChange() {
        m_actionID = TriggerActionID.TAct_OptionChange;
    }
    public override void parseData() {
        m_ConOptionID = (OptionID)Convert.ToInt32(m_data.m_argStr);
    }

    public override void DoIt() {
        if(m_ConOptionID == OptionID.OptionID_None) {
            DebugUtil.LogErrorFormat("[GTActOptionChange] argstr error! ActionID[{0}] [arg]{1}", m_data.m_actionID, m_data.m_argStr);
            return;
        }
        UIMain uimain = (UIMain)UIManager.Ins.GetUI(UIFormID.UI_Main);
        if(uimain == null) {
            DebugUtil.LogErrorFormat("GTActOptionChange Error. UI[UIMain] not found.");
        }
        uimain.AddOptionBtn(m_ConOptionID);
    }
}
/// <summary>
/// NPC对话
/// </summary>
public class GTActNPCTalk : GTAction {
    public GTActNPCTalk() {
        m_actionID = TriggerActionID.TAct_NPCTalk;
    }
    public override void DoIt() {
        throw new NotImplementedException();
    }
}

/// <summary>
/// 打开UI
/// </summary>
public class GTActOpenUI : GTAction {
    public GTActOpenUI() {
        m_actionID = TriggerActionID.TAct_OpenUI;
    }
    public override void DoIt() {
        throw new NotImplementedException();
    }
}

#endregion GTActions
}